package flc.ast.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.TimeUtil;

public class SelAudioAdapter extends StkProviderMultiAdapter<AudioBean> {

    public SelAudioAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(82));
        addItemProvider(new SelAudioProvider());
    }

    private GridLayoutManager gridLayoutManager;

    public void setGridLayoutManager(GridLayoutManager gridLayoutManager) {
        this.gridLayoutManager = gridLayoutManager;
    }

    private int selPosition = -1;

    public int getSelPosition() {
        return selPosition;
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    private boolean isEdit = false;

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private boolean isNoManage;

    public void setNoManage(boolean noManage) {
        isNoManage = noManage;
    }

    private class SelAudioProvider extends BaseItemProvider<AudioBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_audio;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, AudioBean bean) {
            ViewGroup.LayoutParams parm = baseViewHolder.getView(R.id.rlSelAudioItem).getLayoutParams();

            parm.height = gridLayoutManager.getWidth() / gridLayoutManager.getSpanCount()
                    - 2 * baseViewHolder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) parm).leftMargin;
            baseViewHolder.setText(R.id.tvSelAudioItemDuration, TimeUtil.getHHmmss(bean.getDuration()));
            baseViewHolder.setText(R.id.tvSelAudioItemSize, ConvertUtils.byte2FitMemorySize(bean.getSize(), 2));
            baseViewHolder.setText(R.id.tvSelAudioItemTime, TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()), new SimpleDateFormat("MM-dd")));
            if (bean.isSelected()) {
                baseViewHolder.setImageResource(R.id.ivSelAudioItemSel, R.drawable.axuanz);
            } else {
                baseViewHolder.setImageResource(R.id.ivSelAudioItemSel, R.drawable.aweixuanzhong);
            }
            if (isEdit) {
                baseViewHolder.getView(R.id.ivSelAudioItemSel).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.ivSelAudioItemSel).setVisibility(View.GONE);
            }
            if (baseViewHolder.getBindingAdapterPosition() == selPosition) {
                baseViewHolder.setImageResource(R.id.ivSelAudioItemImg, R.drawable.abofangz);
            } else {
                baseViewHolder.setImageResource(R.id.ivSelAudioItemImg, R.drawable.ayinp);
            }
            if (isNoManage) {
                baseViewHolder.getView(R.id.ivSelAudioItemEdit).setVisibility(View.GONE);
            } else {
                baseViewHolder.getView(R.id.ivSelAudioItemEdit).setVisibility(View.VISIBLE);
            }
        }
    }


}
